/********************************************************************************************************
 * @file	ext_hci_uart.c
 *
 * @brief	This is the header file for B91
 *
 * @author	BLE Group
 * @date	2020
 *
 * @par     Copyright (c) 2020, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *          All rights reserved.
 *
 *          Redistribution and use in source and binary forms, with or without
 *          modification, are permitted provided that the following conditions are met:
 *
 *              1. Redistributions of source code must retain the above copyright
 *              notice, this list of conditions and the following disclaimer.
 *
 *              2. Unless for usage inside a TELINK integrated circuit, redistributions
 *              in binary form must reproduce the above copyright notice, this list of
 *              conditions and the following disclaimer in the documentation and/or other
 *              materials provided with the distribution.
 *
 *              3. Neither the name of TELINK, nor the names of its contributors may be
 *              used to endorse or promote products derived from this software without
 *              specific prior written permission.
 *
 *              4. This software, with or without modification, must only be used with a
 *              TELINK integrated circuit. All other usages are subject to written permission
 *              from TELINK and different commercial license may apply.
 *
 *              5. Licensee shall be solely responsible for any claim to the extent arising out of or
 *              relating to such deletion(s), modification(s) or alteration(s).
 *
 *          THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *          ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *          WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *          DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
 *          DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *          (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *          LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *          ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *          (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *          SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *******************************************************************************************************/
#include "ext_hci_uart.h"
#include "common/config/user_config.h"
#include "../uart.h"
#include "../irq.h"
#include "../dma.h"

void HCI_UartSetPin(u16 txPin, u16 rxPin)
{
	uart_gpio_set(txPin, rxPin);
}

void HCI_UartInit(u32 baudrate, u8 *txBuf, u32 len)
{
	/* UART configuration. */
	//uart_gpio_set(HCI_TR_TX_PIN, HCI_TR_RX_PIN);
	uart_reset();

	uart_init_baudrate(baudrate, CLOCK_SYS_CLOCK_HZ, PARITY_NONE, STOP_BIT_ONE);

	uart_recbuff_init(txBuf, len);

	/* UART Rx/Tx use DMA mode. */
	uart_dma_enable(1, 1);

	irq_set_mask(FLD_IRQ_DMA_EN);
	dma_chn_irq_enable(FLD_DMA_CHN_UART_RX, 1);

	irq_set_mask(FLD_IRQ_UART_EN);
	uart_mask_tx_done_irq_enable();
	uart_mask_error_irq_enable();

#if HCI_TR_FLOW_CTRL_EN
	uart_set_rts(true, UART_RTS_MODE_AUTO, HCI_TR_RTS_THRESH, HCI_TR_RTS_INVERT, HCI_TR_RTS_PIN);
	uart_set_cts(true, HCI_TR_CTS_POLARITY, HCI_TR_CTS_PIN);
#endif
}

void HCI_UartSetFlowCtrl(u16 ctsPin, u16 rtsPin)
{
	uart_set_rts(true, UART_RTS_MODE_AUTO, RTS_THRESH, RTS_INVERT, rtsPin);
	uart_set_cts(true, CTS_POLARITY, ctsPin);
}

